package com.link.base.base.product.controller;

import com.link.base.base.common.model.Attachment;
import com.link.base.base.common.service.AttachmentService;
import com.link.base.base.coupons.service.CouponsService;
import com.link.base.base.product.model.Product;
import com.link.base.base.product.service.GuideProdService;
import com.link.base.base.product.service.ProductService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 路江
 * H5 未登录需要调用的方法
 */

@Controller
@RequestMapping("/link/guideproduct")
public class GuideProdController extends BasicController<Product> {

    @Resource
    private ProductService productService;

    @Resource
    private CouponsService couponsService;

    @Resource
    private AttachmentService attachmentService;

    @Resource
    private GuideProdService guideProdService;

    /**
     * 查询单个产品 queryById
     * @author lujiang
     */
    @Override
    public Map<String, Object> queryById(@RequestBody Product entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            entity.setLoginCorpId(entity.getLoginCorpId());
            entity.setCorpid(entity.getLoginCorpId());
            entity.setAnonymousFlag("Y");
            Product record = guideProdService.queryById(entity);
            Attachment attachment = new Attachment();
            attachment.setHeadId(entity.getId());
            attachment.setPageFlag(false);
            attachment.setLoginCorpId(entity.getLoginCorpId());
            //产品附件轮播图
            List<Attachment> attachmentList = attachmentService.queryByExamplePage(attachment);
            for (Attachment att : attachmentList) {
                att.setAnonymousFlag("Y");
            }
            record.setAttachmentList(attachmentList);
            record.setAnonymousFlag("Y");
            List<Product> list = new ArrayList<>();
            list.add(record);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 生成H5分享小程序码
     *
     * @author lujiang
     */
    @RequestMapping(value = "/createGuideCode")
    @ResponseBody
    public Map<String, Object> createGuideCode(@RequestBody Product product, HttpSession session) {
        Map<String, Object> map = new HashMap<>(8);
        try {
            map = productService.createMiniprogramCode(product);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }
}
